const app = getApp()
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
    key: 'O26BZ-2G2C5-UKNIM-I6WEW-5GHF3-TWBJ5' // 必填
});
import {
    getById
} from "../../api/api.js"
Page({
    data: {
        height: 568,
        markers: [],
        polyline: [],
        result: [],
        points: [],
        longitude: '',
        latitude: ''
    },
    getHeight: function () {
        let that = this;
        wx.getSystemInfo({
            success: function (res) {
                let clientHeight = res.windowHeight;
                let clientWidth = res.windowWidth;
                let ratio = 750 / clientWidth;
                let height = clientHeight * ratio;
                that.setData({
                    height: height
                });
            }
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      console.log(options)
        var that = this
        //获取当前位置
        wx.getLocation({
            type: 'wgs84',
            success(res) {
                const latitude = res.latitude
                const longitude = res.longitude
                that.setData({
                    longitude,
                    latitude
                })
                getById({
                    id: options.id
                }).then((res) => {
                    let datas = res.data || {};
                    console.log('123456')
                    let options = {
                        start: {
                            longitude,
                            latitude
                        },
                        end: {
                            longitude: Number(datas.longitude),
                            latitude: Number(datas.latitude),
                        }
                    }
                    that.line(options.start, options.end);
                    var marker = [{
                            iconPath: "../../static/icon5.png",
                            id: 0,
                            ...options.start,
                            width: 33,
                            height: 33,
                            callout: {
                                content: '我的位置',
                                color: '#0000ff',
                                fontSize: 14,
                                borderRadius: 10,
                                bgColor: '#fff',
                                padding: 5,
                                display: 'ALWAYS',
                                textAlign: 'center'
                            },
                        },
                        {
                            iconPath: "../../static/icon6.png",
                            id: 2,
                            ...options.end,
                            width: 33,
                            height: 33,
                            callout: {
                                content: datas.parkName,

                                color: '#ff0000',
                                fontSize: 14,
                                borderRadius: 10,
                                bgColor: '#fff',
                                padding: 5,
                                display: 'ALWAYS',
                                textAlign: 'center'
                            }
                        }
                    ]
                    var points_ = [{
                            ...options.start,
                        },
                        {
                            ...options.end,
                        }
                    ]
                    that.setData({
                        markers: marker,
                        points: points_,
                    })
                })
            }
        })





    },
    onReady: function () {
        this.mapCtx = wx.createMapContext('myMap');
    },
    line: function (start, end) {
        var _this = this
        qqmapsdk.direction({
            mode: 'driving', //可选值：'driving'（驾车）、'walking'（步行）、'bicycling'（骑行），不填默认：'driving',可不填
            from: start,
            to: end,
            success: function (res, data) {
                console.log(res);
                var ret = res;
                var coors = ret.result.routes[0].polyline,
                    pl = [];
                //坐标解压（返回的点串坐标，通过前向差分进行压缩）
                var kr = 1000000;
                for (var i = 2; i < coors.length; i++) {
                    coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
                }
                //将解压后的坐标放入点串数组pl中
                for (var i = 0; i < coors.length; i += 2) {
                    pl.push({
                        latitude: coors[i],
                        longitude: coors[i + 1]
                    })
                }
                console.log(pl)
                //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
                _this.setData({
                    latitude: pl[0].latitude,
                    longitude: pl[0].longitude,
                    polyline: [{
                        points: pl,
                        color: "#37C4FF",
                        width: 4,
                        dottedLine: false
                    }]
                })
            },
            fail: function (error) {
                console.error(error);
            },
            complete: function (res) {
                console.log(res);
            }
        });
    },
})